/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.shared.util;

import java.awt.Color;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author BullionXpress.com
 */
public class Utility {

    public static synchronized long generateId() {
        try {
            Thread.sleep(1);
        } catch (InterruptedException ex) {
        }
        return System.currentTimeMillis();
    }

    public static boolean isValidUserID(String userID)
    {
        if( userID == null ){
            return false;
        } else if( userID.length() < 6 ){
            return false;
        } else if( userID.length() > 64 ){
            return false;
        } 
        return true;
    }

    public static boolean isValidQuantity(String quantity)
    {
        try {
            Integer v = Integer.parseInt(quantity);
            if( v <= 0 ){
                return false;
            }
            return true;
        } catch(Exception ex){
            return false;
        }
    }

    public static boolean isValidExpiry(String expiry)
    {
        try {
            if( expiry == null || expiry.equals("")){
                return true;
            } else {
                SimpleDateFormat fmt = new SimpleDateFormat("dd-mm-yyyy");
                fmt.parse(expiry);
                return true;
            }
        } catch(Exception ex){
            return false;
        }
    }

    public static boolean isValidLimitPrice(String limitPrice)
    {
        if( limitPrice == null || limitPrice.equals("")){
            return false;
        }
        try {
            double d = Double.parseDouble(limitPrice);
            if( d >= 0 ){
                return true;
            }
        } catch(Exception ex){
        }
        return false;
    }

    public static boolean isValidPassword(String password) {
        if (password == null) {
            return false;
        }
        if (password.length() < 6) {
            return false;
        }
        if (password.length() > 21) {
            return false;
        }
        if (!password.matches("[a-zA-Z0-9]*")) {
            return false;
        }
        if (password.matches("[a-zA-Z]*99")) {
            return false;
        }
        return true;
    }

    public static boolean isValidEmail(String email) {
        Pattern pattern;
        Matcher matcher;
        final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /** isPhoneNumberValid: Validate phone number using Java reg ex.
     * This method checks if the input string is a valid phone number.
     * @param email String. Phone number to validate
     * @return boolean: true if phone number is valid, false otherwise.
     */
    public static boolean isValidTelephone(String phoneNumber) {
        boolean isValid = false;
        /* Phone Number formats: (nnn)nnn-nnnn; nnnnnnnnnn; nnn-nnn-nnnn
        ^\\(? : May start with an option "(" .
        (\\d{3}): Followed by 3 digits.
        \\)? : May have an optional ")"
        [- ]? : May have an optional "-" after the first 3 digits or after optional ) character.
        (\\d{3}) : Followed by 3 digits.
        [- ]? : May have another optional "-" after numeric digits.
        (\\d{4})$ : ends with four digits.

        Examples:
        (123)456-7890, 123-456-7890, 1234567890, (123)-456-7890

         */
        //Initialize reg ex for phone number.
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    public static Color getColor(String hex) {
        hex = hex.trim();
        hex = hex.toLowerCase();
        hex = hex.replace("#", "");
        int intValue = Integer.parseInt(hex, 16);
        Color aColor = new Color(intValue);
        return aColor;
    }

    public static double addPercent(double amount, double percent) {
        double retVal = 0;
        retVal = (amount / 100) * percent;
        retVal = retVal + amount;
        return retVal;
    }

    public static double subtractPercent(double amount, double percent) {
        double retVal = 0;
        retVal = (amount / 100) * percent;
        retVal = retVal - amount;
        return retVal;
    }
    
    public static Date sqlDateNow() { 
        java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        return sqlDate;     
    }    

    
}
